/*
 * Client
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>


#define SIZE 100

int main (int argc,char* argv[])
{
	
	/*
	 *Variable du client 
	 */
	int PORT = 9600;
	 char host[16];

	 bzero(host, sizeof(host));
	 strcpy (host, "127.0.0.1");
	 
	 
	if (argc == 2)
		PORT = atoi(argv[1]);
	else if (argc == 3)
	{
		strcpy (host, argv[1]);	
		PORT = atoi(argv[2]);
	}

	printf ("tapez EXIT pour sortir, sinon, amusez-vous bien de chatter avec le server\n");
	char ch[SIZE] = "";
	char response[SIZE] = "";



	int len_client =0;
	int sockfd = 0;
	struct sockaddr_in adress;
	
	sockfd = socket(AF_INET, SOCK_STREAM, 0);

		adress.sin_family = AF_INET;
		adress.sin_addr.s_addr = inet_addr(host);
		adress.sin_port = PORT;
		len_client = sizeof(adress);
		
		connect(sockfd, (struct sockaddr *)(&adress) ,len_client);


	while (strcmp (response, "GOODBYE"))
	{
		
		printf ("client: ");
		fgets (ch, SIZE, stdin);
		printf (" ");
		write(sockfd, ch, (strlen(ch) - 1));
		memset (response, SIZE, 0);
		read(sockfd, &response, SIZE);
		printf("server: %s \n", response);

	}
	close(sockfd);
		
	return 0;
}
